<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="garageName">
        <el-input
          v-model="queryParams.garageName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable
                        v-model="queryParams.auditTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="接单状态 " prop="receivingOrder">
        <el-select v-model="queryParams.receivingOrder" placeholder="请选择接单状态 " clearable>
          <el-option
            v-for="dict in dict.type.receiving_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.user_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['function:garage:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['function:garage:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['function:garage:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['function:garage:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="garageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商家id" align="center" prop="id"/>
      <el-table-column label="店铺名称" align="center" prop="garageName"/>
      <el-table-column label="头像" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺介绍" align="center" prop="introduction"/>
      <el-table-column label="服务类型" align="center" prop="serviceTypeIds"/>
      <el-table-column label="服务区域" align="center" prop="serviceAreas"/>
      <el-table-column label="营业执照图片" align="center" prop="businessLicense" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.businessLicense" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="道路运输许可证图片" align="center" prop="transportLicense" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.transportLicense" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="门店照片" align="center" prop="shopPhotos" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shopPhotos" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="contactMobile"/>
      <el-table-column label="紧急联系人电话" align="center" prop="emergencyMobile"/>
      <el-table-column label="省份" align="center" prop="province"/>
      <el-table-column label="城市" align="center" prop="city"/>
      <el-table-column label="区域" align="center" prop="district"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="经度" align="center" prop="longitude"/>
      <el-table-column label="纬度" align="center" prop="latitude"/>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="auditRemark"/>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资质证书照片" align="center" prop="qualificationPhotos" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.qualificationPhotos" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺环境照片" align="center" prop="environmentPhotos" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.environmentPhotos" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="验证状态" align="center" prop="businessLicenseStatus"/>
      <el-table-column label="验证备注" align="center" prop="businessLicenseRemark"/>
      <el-table-column label="公司名称" align="center" prop="companyName"/>
      <el-table-column label="统一社会信用代码" align="center" prop="creditCode"/>
      <el-table-column label="法人姓名" align="center" prop="legalPerson"/>
      <el-table-column label="接单状态 " align="center" prop="receivingOrder">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.receiving_status" :value="scope.row.receivingOrder"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_state" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="score"/>
      <el-table-column label="总评分" align="center" prop="totalScore"/>
      <el-table-column label="评价次数" align="center" prop="scoreCount"/>
      <el-table-column label="平均评分" align="center" prop="avgScore"/>
      <el-table-column label="五星好评率" align="center" prop="fiveStarRate"/>
      <el-table-column label="完成订单数" align="center" prop="completeOrderCount"/>
      <el-table-column label="工时" align="center" prop="workingHours"/>
      <el-table-column label="工位数量" align="center" prop="workstationsNumber" />
      <el-table-column label="级别" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.workshop_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="时段价" align="center" prop="hourPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['function:garage:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            v-if="scope.row.auditStatus === 1"
            v-hasPermi="['function:garage:approve']"
          >审批
          </el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['function:garage:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入关联用户ID"/>
        </el-form-item>
        <el-form-item label="店铺名称" prop="garageName">
          <el-input v-model="form.garageName" placeholder="请输入店铺名称"/>
        </el-form-item>

        <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="店铺介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceTypeIds">
          <el-input v-model="form.serviceTypeIds" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

        <el-form-item label="服务区域" prop="serviceAreas">
          <el-input v-model="form.serviceAreas" placeholder="请输入服务区域"/>
        </el-form-item>
        <el-form-item label="营业执照图片" prop="businessLicense">
          <image-upload v-model="form.businessLicense"/>
        </el-form-item>
        <el-form-item label="道路运输许可证图片" prop="transportLicense">
          <image-upload v-model="form.transportLicense"/>
        </el-form-item>
        <el-form-item label="门店照片" prop="shopPhotos">
          <image-upload v-model="form.shopPhotos"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactMobile">
          <el-input v-model="form.contactMobile" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="紧急联系人电话" prop="emergencyMobile">
          <el-input v-model="form.emergencyMobile" placeholder="请输入紧急联系人电话"/>
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份"/>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市"/>
        </el-form-item>
        <el-form-item label="区域" prop="district">
          <el-input v-model="form.district" placeholder="请输入区域"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度"/>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="form.auditRemark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
                          v-model="form.auditTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资质证书照片" prop="qualificationPhotos">
          <image-upload v-model="form.qualificationPhotos"/>
        </el-form-item>
        <el-form-item label="店铺环境照片" prop="environmentPhotos">
          <image-upload v-model="form.environmentPhotos"/>
        </el-form-item>
        <el-form-item label="验证备注" prop="businessLicenseRemark">
          <el-input v-model="form.businessLicenseRemark" placeholder="请输入验证备注"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称"/>
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入法人姓名"/>
        </el-form-item>
        <el-form-item label="接单状态 " prop="receivingOrder">
          <el-select v-model="form.receivingOrder" placeholder="请选择接单状态 ">
            <el-option
              v-for="dict in dict.type.receiving_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.user_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="工位数量" prop="workstationsNumber">
          <el-input v-model="form.workstationsNumber" placeholder="请输入工位数量" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择级别">
            <el-option
              v-for="dict in dict.type.workshop_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时段价" prop="hourPrice">
          <el-input v-model="form.hourPrice" placeholder="请输入时段价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="approvaLopen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="店铺名称" prop="garageName">
          <el-input v-model="form.garageName" :disabled="true"/>
        </el-form-item>
        <el-form-item label="头像" prop="shopPhotos">
          <image-upload v-model="form.avatar" :disabled="true"/>
        </el-form-item>
        <el-form-item label="店铺介绍" prop="introduction">
          <el-input v-model="form.introduction" :disabled="true"/>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceTypeIds">
          <el-input v-model="form.serviceTypeIds" :disabled="true"/>
        </el-form-item>
        <el-form-item label="服务区域" prop="serviceAreas">
          <el-input v-model="form.serviceAreas" :disabled="true"/>
        </el-form-item>
        <el-form-item label="营业执照图片" prop="businessLicense">
          <image-upload v-model="form.businessLicense"/>
        </el-form-item>
        <el-form-item label="道路运输许可证图片" prop="transportLicense">
          <image-upload v-model="form.transportLicense" :disabled="true"/>
        </el-form-item>
        <el-form-item label="门店照片" prop="shopPhotos">
          <image-upload v-model="form.shopPhotos" :disabled="true"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactMobile">
          <el-input v-model="form.contactMobile" :disabled="true"/>
        </el-form-item>
        <el-form-item label="紧急联系人电话" prop="emergencyMobile">
          <el-input v-model="form.emergencyMobile" :disabled="true"/>
        </el-form-item>
        <el-form-item label="区域" prop="district">
          <el-input v-model="form.district" :disabled="true"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" :disabled="true"/>
        </el-form-item>
        <el-form-item label="资质证书照片" prop="qualificationPhotos">
          <image-upload v-model="form.qualificationPhotos"  :disabled="true"/>
        </el-form-item>
        <el-form-item label="店铺环境照片" prop="environmentPhotos">
          <image-upload v-model="form.environmentPhotos"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" :disabled="true"/>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" :disabled="true"/>
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPerson">
          <el-input v-model="form.legalPerson" :disabled="true"/>
        </el-form-item>

        <el-form-item label="工位数量" prop="workstationsNumber">
          <el-input v-model="form.workstationsNumber" placeholder="请输入工位数量" :disabled="true" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择级别">
            <el-option
              v-for="dict in dict.type.workshop_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时段价" prop="hourPrice">
          <el-input v-model="form.hourPrice" placeholder="请输入时段价" :disabled="true" />
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="form.auditRemark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="approvaForm(2)">审批通过</el-button>
        <el-button type="primary" @click="approvaForm(3)">拒 绝</el-button>
        <el-button @click="approvaCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listGarage, getGarage, delGarage, addGarage, updateGarage, putCheck} from "@/api/function/garage";

export default {
  name: "Garage",
  dicts: ['user_state', 'audit_status', 'receiving_status', 'service_type', 'workshop_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户表格数据
      garageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      approvaLopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        garageName: null,
        auditStatus: null,
        auditTime: null,
        qualificationPhotos: null,
        environmentPhotos: null,
        businessLicenseStatus: null,
        businessLicenseRemark: null,
        companyName: null,
        creditCode: null,
        legalPerson: null,
        receivingOrder: null,
        status: null,
        score: null,
        totalScore: null,
        scoreCount: null,
        avgScore: null,
        fiveStarRate: null,
        completeOrderCount: null,
        workingHours: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "关联用户ID不能为空", trigger: "blur"}
        ],
        garageName: [
          {required: true, message: "店铺名称不能为空", trigger: "blur"}
        ],
        auditStatus: [
          {required: true, message: "审核状态不能为空", trigger: "change"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        businessLicenseStatus: [
          {required: true, message: "验证状态不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商户列表 */
    getList() {
      this.loading = true;
      listGarage(this.queryParams).then(response => {
        this.garageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    approvaCancel() {
      this.approvaLopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        garageName: null,
        avatar: null,
        introduction: null,
        serviceTypeIds: null,
        serviceAreas: null,
        businessLicense: null,
        transportLicense: null,
        shopPhotos: null,
        contactMobile: null,
        emergencyMobile: null,
        province: null,
        city: null,
        district: null,
        address: null,
        longitude: null,
        latitude: null,
        auditStatus: null,
        auditRemark: null,
        auditTime: null,
        createTime: null,
        updateTime: null,
        qualificationPhotos: null,
        environmentPhotos: null,
        businessLicenseStatus: null,
        businessLicenseRemark: null,
        companyName: null,
        creditCode: null,
        legalPerson: null,
        receivingOrder: null,
        status: null,
        score: null,
        totalScore: null,
        scoreCount: null,
        avgScore: null,
        fiveStarRate: null,
        completeOrderCount: null,
        workingHours: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGarage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户";
      });
    },
    /** 审批按钮操作 */
    handleApprove(row) {
      this.reset();
      const id = row.id || this.ids
      getGarage(id).then(response => {
        this.form = response.data;
        this.approvaLopen = true;
        this.title = "审批商户商户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGarage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGarage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审批按钮 */
    approvaForm(i) {
      this.form.auditStatus=i;
      console.log(this.form.auditStatus)
      if (i === 3 && !this.form.auditRemark) {
        this.$modal.msgError("审批不通过时备注必填");
        return;
      }
      putCheck(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.approvaLopen = false;
        this.getList();
      });


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商户编号为"' + ids + '"的数据项？').then(function () {
        return delGarage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('function/garage/export', {
        ...this.queryParams
      }, `garage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
